/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.easypr.organizer.shared.AppConstants;
import org.easypr.organizer.shared.MyUser;

import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gwt.core.client.GWT;

// TODO: Auto-generated Javadoc
/**
 * The Class MyOAuthServlet.
 */
@SuppressWarnings("serial")
public class MyOAuthServlet extends HttpServlet {

	
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		HttpSession session;
		session = request.getSession();
		MyUser user = (MyUser) session.getAttribute("User");
		
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
		oauthParameters.setOAuthConsumerKey(AppConstants.OAuthConsumerKey);
	    oauthParameters.setOAuthConsumerSecret(AppConstants.OAuthConsumerSecret);
	    
	    try {
	    	GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
		    oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters);
		    
		    try {
		    	
		    	oauthParameters.setOAuthTokenSecret(user.getOAuthTokenSecret());
		    	
		    	
		    	String accessToken = oauthHelper.getAccessToken(oauthParameters);
		    	
		    	
		    	String accessTokenSecret = oauthParameters.getOAuthTokenSecret();
		    	
		    	
		    	user.setAccessToken(accessToken);
		    	// System.out.println("accesstoken: " + accessToken);
		    	user.setOAuthTokenSecret(accessTokenSecret);
		    	// Uppdaterar session
		    	session.removeAttribute("User");
		    	session.setAttribute("User", user);
		    	response.sendRedirect(AppConstants.domain+"/#import");
				
		    	
		    } catch (OAuthException oex) {
		    	System.out.println(oex.getMessage());
		    	System.out.println("Exception i getOAuthAccessToken!");
		    }
	    } catch (Exception e) {
	    	System.out.println("slutet: ");
	    	System.out.println(e.getMessage());
	    }
	}
}
